<template>
	<view class="login-page">
		<view class="top-len flex-end">
		</view>

		<view class="middle-logo flex-around flex-column">
			<image class="logo" src="../../static/logo/642.png"></image>
			<text class="logotext">您的专属货币商城</text>
		</view>

		<view class="content">
			<view class="margin-20-0">
				<u-cell-group>
					<u-field v-model="phone" label="手机号" placeholder="请填写手机号"></u-field>
					<u-field v-model="code" label="验证码" placeholder="请填写验证码">
						<u-button size="mini" slot="button" type="success" @tap="getsmscode">{{smsbtn.text}}</u-button>
					</u-field>
					<u-verification-code ref="uCode" @change="codeChange"></u-verification-code>
				</u-cell-group>
				<u-button class="margin-20-0" type="primary" @tap="setin">登录</u-button>
				<navigator url="register" hover-class="">
					<text>注册会员</text>
				</navigator>
			</view>
		</view>

		<view class="footer">
			<text>登录即默认同意</text>
			<navigator url="/pages/login/UserAgreement" class="link">
				<text class="link">《用户协议》</text>
			</navigator>
		</view>
	</view>
</template>

<script>
	// import { } from 'vux';
	export default {
		name: "Login",
		components: {},
		data() {
			return {
				phone: '',
				code: '',
				smsbtn: {
					text: '获取验证码',
					status: false,
					codeTime: 180,
				},
				timerId: null,
				userinfo: []
			};
		},
		onLoad() {
			let phones = uni.getStorageSync('loginphone');
			if (phones) {
				this.phone = phones;
			}
		},
		methods: {
			codeChange(text) {
				this.code = text;
			},
			getsmscode: function() {
				var vm = this;
				if (this.smsbtn.status == true) {
					uni.showToast({
						title: '别着急！短信已经发送了~',
						icon: 'none',
						mask: true
					})
					return false;
				}
				uni.showToast({
					title: '已发送',
					icon: 'none',
					mask: true
				})
				vm.smsbtn.status = true;
				vm.timerId = setInterval(() => {
						var codeTime = vm.smsbtn.codeTime;
						codeTime--;
						vm.smsbtn.codeTime = codeTime;
						vm.smsbtn.text = codeTime + "秒";
						if (codeTime < 1) {
							clearInterval(vm.timerId);
							vm.smsbtn.text = "重新获取";
							vm.smsbtn.codeTime = 180;
							vm.smsbtn.status = false;
						}
					},
					1000);
				return false;
			},
			setin() {
				// uni.redirectTo({
				// 	url:'../shop/shop'
				// });
				// return;
				uni.request({
					url: this.$global.APIURL + 'login/mobile',
					method: 'POST',
					data: {
						phone: this.phone,
						code: this.code,
						spread: ''
					},
					dataType: 'json',
					header: {
						'content-type': 'application/json;charset=UTF-8',
					},
					success: res => {
						var data = res.data;
						this.$store.commit("LOGIN", data.token, dayjs(data.expires_time));
						const backUrl = cookie.get(BACK_URL) || "/";
						cookie.remove(BACK_URL);
						this.$router.replace({ path: backUrl });
						
						if (rest.status == 'error') {
							uni.showToast({
								title: rest.msg,
								icon: 'none',
								mask: true
							})
						} else if (rest.status == 'success') {
							this.userinfo = rest.msg;
							const data = {
								foxtel: this.phone,
								foxcode: this.code,
								foxtoken: this.userinfo['user_key'],
								foxactive: true
							}
							uni.clearStorageSync();
							this.service.addfox(data);
							uni.navigateTo({
								url: '../index/index'
							})
						} else if (rest.status == 'password') {
							this.userinfo = rest.msg;
							const data = {
								foxtel: this.phone,
								foxcode: this.code,
								foxtoken: this.userinfo['user_key'],
								foxactive: true
							}
							uni.clearStorageSync();
							this.service.addfox(data);
							uni.reLaunch({
								url: './set_password'
							})
						}
					}
				})
			},
			loginAuth: function(ttype) {
				// #ifdef H5
				var userInfo = {
					loginType: 'qq',
					nickname: 'Spanky',
					gender: '男',
					avatar: 'http://qzapp.qlogo.cn/qzapp/1105975417/4AAA6E621D0940D893B26C7098B47C2D/100',
					openId_qq: '4AAA6E621D0940D893B26C7098B47C2D',
					openId_wx: ''
				};
				this.$store.commit("userStatus", userInfo);
				uni.setStorageSync('userStatus', userInfo);
				// uni.redirectTo({url: '/pages/index/index'});
				uni.navigateBack({});
				return;
				// #endif
				var vm = this;
				uni.showToast({
					title: "",
					icon: "loading"
				});
				uni.login({
					provider: ttype,
					success: function(loginRes) {
						console.log(loginRes.authResult);
						// 获取用户信息
						uni.getUserInfo({
							provider: ttype,
							success: function(infoRes) {
								console.log(infoRes);
								var userInfo = {
									loginType: ttype,
									nickname: infoRes.userInfo.nickName,
									gender: vm.genderFmt(infoRes.userInfo.gender),
									avatar: infoRes.userInfo.figureurl_2 || infoRes.userInfo.avatarUrl,
									openId_qq: ttype == 'qq' ? infoRes.userInfo.openId : '',
									openId_wx: ttype == 'weixin' ? infoRes.userInfo.openId : ''
								};
								uni.hideToast();
								vm.$store.commit("userStatus", userInfo);
								uni.setStorageSync('userStatus', userInfo);
								uni.navigateBack({});
							},
							fail: function(res) {
								uni.showToast({
									title: "授权失败",
									position: 'bottom'
								});
							}
						});
					}
				});
			}
		},
		onNavigationBarButtonTap(e) {
			// let pages = getCurrentPages();  // 获取所有页面栈实例列表
			// let nowPage = pages[ pages.length - 1];  // 当前页页面实例
			// let prevPage = pages[ pages.length - 2 ];  // 上一页页面实例
			// prevPage.$vm.searchVal = 1211;
			// 返回按钮点击
			uni.navigateBack({
				delta: 1
			});
		},
		mounted: function() {
			// #ifdef H5  
			var currentWebview = this.$mp.page.$el.classList.add('noback');
			// #endif
		}
	};
</script>

<style lang="scss" scoped>
	@import './login.scss';

	[data-page~="pages/login/login"] .uni-page-head-hd {
		visibility: hidden;
	}
</style>
